package subjectservicelogic

import (
	"context"
	"database/sql"
	"time"

	"github.com/blog-zero/rpc/cms/cmsclient"
	"github.com/blog-zero/rpc/cms/internal/svc"
	"github.com/blog-zero/rpc/model/cmsmodel"

	"github.com/zeromicro/go-zero/core/logx"
)

type SubjectAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSubjectAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SubjectAddLogic {
	return &SubjectAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 专题
func (l *SubjectAddLogic) SubjectAdd(in *cmsclient.SubjectAddReq) (*cmsclient.SubjectAddResp, error) {
	item := &cmsmodel.CmsSubject{
		CategoryId:      in.CategoryId,
		Title:           in.Title,
		Pic:             in.Pic,
		ProductCount:    in.ProductCount,
		RecommendStatus: in.RecommendStatus,
		CreateTime:      time.Now(),
		CollectCount:    in.CollectCount,
		ReadCount:       in.ReadCount,
		CommentCount:    in.CommentCount,
		AlbumPics:       in.AlbumPics,
		Description:     sql.NullString{String: in.Description, Valid: true},
		ShowStatus:      in.ShowStatus,
		Content:         in.Content,
		ForwardCount:    in.ForwardCount,
		CategoryName:    in.CategoryName,
	}
	if _, err := l.svcCtx.CmsSubjectModel.Insert(l.ctx, item); err != nil {
		return nil, err
	}

	return &cmsclient.SubjectAddResp{}, nil
}
